<?php
class ModelContentContent extends Model {
	public function addContent($data, $uploadfile) {
		if (isset($uploadfile) && $uploadfile != '') {
			$this->db->query("INSERT INTO content SET pid = '" . $this->db->escape($data['menu_id']) 
			. "', title = '" . $this->db->escape($data['title']) 
			. "', summary = '" . $this->db->escape($data['summary']) 
			. "', detail = '" . $this->db->escape($data['detail']) 
			//. "', detail = '" . $data['detail'] 
			. "', image = '" . $this->db->escape($uploadfile)
			. "', ordering = '" . $this->db->escape($data['ordering']) 
			. "', createdate = '" . $this->db->escape($data['add_date']) 
			. "'");
		} else {
			$this->db->query("INSERT INTO content SET pid = '" . $this->db->escape($data['menu_id']) 
			. "', title = '" . $this->db->escape($data['title']) 
			. "', summary = '" . $this->db->escape($data['summary']) 
			. "', detail = '" . $this->db->escape($data['detail']) 
			//. "', detail = '" . $data['detail'] 
			. "', ordering = '" . $this->db->escape($data['ordering']) 
			. "', createdate = '" . $this->db->escape($data['add_date']) 
			. "'");
		}
	}
	
	public function editContent($content_id, $data, $uploadfile) {
		if (isset($uploadfile) && $uploadfile != '') {
			$this->db->query("UPDATE content SET pid = '" . $this->db->escape($data['menu_id'])  
			. "', title = '" . $this->db->escape($data['title']) 
			. "', summary = '" . $this->db->escape($data['summary']) 
			. "', detail = '" . $this->db->escape($data['detail']) 
			. "', image = '" . $this->db->escape($uploadfile)
			. "', ordering = '" . $this->db->escape($data['ordering']) 
			. "', createdate = now() "
			. " WHERE content_id = '" . (int)$content_id . "'");
		} else {
			$this->db->query("UPDATE content SET pid = '" . $this->db->escape($data['menu_id'])  
			. "', title = '" . $this->db->escape($data['title']) 
			. "', summary = '" . $this->db->escape($data['summary']) 
			. "', detail = '" . $this->db->escape($data['detail']) 
			. "', ordering = '" . $this->db->escape($data['ordering']) 
			. "', updatedate = now() "
			. " WHERE content_id = '" . (int)$content_id . "'");
		}
		
	}
	
	public function deleteContent($content_id) {
		$this->db->query("DELETE FROM content WHERE content_id = '" . (int)$content_id . "'");
	}
	
	public function getContentById($content_id) {
		$query = $this->db->query("SELECT * FROM content WHERE content_id = '" . (int)$content_id . "'");
	
		return $query->row;
	}
	
	public function getTotalContents($data = array()) {
		$sql = "SELECT COUNT(*) AS total FROM content WHERE pid = " . $data['menu_id'];

		$query = $this->db->query($sql);

		return $query->row['total'];
	}
	
	public function getContents($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM content ";

			$sort_data = array(
			'title',
			'ordering',
			'createdate',
			'hit',
			'content_id'
			);

			if (isset($data['menu_id'])) {
				$sql .= " WHERE pid = " . $data['menu_id'];
			}
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];
			} else {
				$sql .= " ORDER BY title";
			}
				
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}

			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}
					
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}
				
			$query = $this->db->query($sql);

			return $query->rows;
		} else {
			$content_data = $this->cache->get('content.' . $this->config->get('config_language_id'));

			if (!$content_data) {
				$query = $this->db->query("SELECT * FROM content WHERE pid = " . $data['menu_id']);

				$content_data = $query->rows;
					
				$this->cache->set('content.' . $this->config->get('config_language_id'), $content_data);
			}

			return $content_data;
		}
	}
}
?>